%{
#include <stdio.h>
#include <string.h>

#include "common.h"
#include "ident.h"

char *cur_filename = "<stdin>";
int lineno = 1;
 
void yyerror(const char *str) {
	ferrf("%s:%d: %s", cur_filename, lineno, str);
}
 
int yywrap() {
        return 1;
} 

%}

%token USE MODULE

%token NUMBER
%token IDENT
%token QCHAR QSTRING

%token LARROW

%left DOT

%token LPAREN RPAREN
%token LBRACKET RBRACKET
%token LBRACE RBRACE

%left ADD SUB MULT DIV
%left EQUALS NEQUALS
%left NOT AND OR
%left BAND BOR BXOR

%token SEMI

%start program

%%

program:
	preface statements
	;

preface:
	module_declaration use_declarations
	;

statements:
	/* empty */
	|
	statements statement
	;

module_declaration:
	MODULE ident semi
	;

use_declarations:
	/* empty */
	|
	use_declarations use_declaration
	;

use_declaration:
	USE ident semi
	;

statement:
	semi { /* do nothing */ }
	|
	ident LARROW expression semi
	;

semi:
	SEMI
	;

val:
	NUMBER
	|
	QCHAR
	|
	QSTRING
	;

ident:
	IDENT DOT ident
	|
	IDENT
	;

expression:
	ident
	|
	val
	|
	LPAREN expression RPAREN
	|
	additive_expression
	|
	multiplicative_expression
	|
	boolean_expression
	|
	binary_expression
	;

additive_expression:
	expression ADD expression
	|
	expression SUB expression
	;

multiplicative_expression:
	expression MULT expression
	|
	expression DIV expression
	;

boolean_expression:
	expression EQUALS expression
	|
	expression NEQUALS expression
	|
	expression AND expression
	|
	expression OR expression
	|
	NOT expression
	;

binary_expression:
	expression BAND expression
	|
	expression BOR expression
	|
	expression BXOR expression
	;

